﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Maxleaf.EDI.Domain;
using Maxleaf.EDI.Domain.EDI;

namespace Maxleaf.EDI.Core.Handlers.QueryHandlers
{
    public class GetAllDBsQueryHandler : IQueryHandler<Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsParameters, Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsResult>
    {
        private readonly IUnitOfWorkProvider unitOfWorkProvider;
        private readonly IRepository<DBDesc> dbRepository;

        public GetAllDBsQueryHandler(
            IRepository<DBDesc> dbRepository,
            IUnitOfWorkProvider unitOfWorkProvider)
        {
            this.dbRepository = dbRepository;
            this.unitOfWorkProvider = unitOfWorkProvider;
        }

        public Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsResult Handle(Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsParameters parameters)
        {
            using (var unitOfWork = this.unitOfWorkProvider.GetEDIUnitOfWork())
            {
                var dbs = this.dbRepository.GetAll(unitOfWork).Where(o => o.FDeleted == 0);

                return new Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsResult() { DBs = dbs.Select(o => new Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsResult.DB { FInterId=o.Finterid, DBId=o.DBID, DBName=o.DBName}).ToList() };
            }
        }
    }
}
